<template>
	<view class="section" :data-theme="theme">
		<!-- <view class='line'>
			<image src='../../address/static/images/line.png' v-if="invoicerecordList.length"></image>
		</view> -->
		<view class='address-management' :class='invoicerecordList.length < 1 && page > 1 ? "fff":""'>
			<radio-group class="radio-group" @change="radioChange" v-if="invoicerecordList.length">
				<view class='item borRadius14' v-for="(item,index) in invoicerecordList" :key="index">
					<view class='address'>
            <view v-if="item.businessAddress">个体工商户开票</view>
            <view v-else>自然人开票</view>
						<view class='consignee' v-if="item.businessAddress">{{item.realName}}</view>
						<view class='consignee' v-else>{{item.cardholder}}</view>
					</view>
					<view class='operation acea-row row-between-wrapper'>
						<view class='acea-row row-middle'>
							<view @click='editInvoice(item.id,item.businessAddress)'><text class='iconfont icon-bianji'></text>编辑</view>
							<view @click='delInvoice(index)'><text class='iconfont icon-shanchu'></text>删除</view>
						</view>
					</view>
				</view>
			</radio-group>
			<view class='loadingicon acea-row row-center-wrapper'>
				<text class='loading iconfont icon-jiazai' :hidden='loading==false'></text>
			</view>
			<emptyPage v-if="invoicerecordList.length == 0 && !loading" title="暂无开票信息~" :imgSrc="urlDomain+'crmebimage/presets/noShopper.png'"></emptyPage>
			<view style='height:120rpx;'></view>
		</view>
		<view class='footer acea-row row-between-wrapper'>
			<!-- #ifdef APP-PLUS -->
			<view class='addressBnt bg_color on' @click='addInvoice'><text
					class='iconfont icon-tianjiadizhi'></text>添加开票信息</view>
			<!-- #endif -->
			<!-- #ifdef MP-->
			<view class='addressBnt bg_color' @click='addInvoice'><text class='iconfont icon-tianjiadizhi'></text>添加开票信息
			</view>
			<!-- #endif -->
			<!-- #ifdef H5-->
			<view class='addressBnt bg_color' :class="this.$wechat.isWeixin()?'':'on'" @click='addInvoice'><text
					class='iconfont icon-tianjiadizhi'></text>添加开票信息</view>
			<!-- #endif -->
		</view>
	</view>
</template>

<script>
	import {
		getInvoicerecord,
    delInvoicerecord
	} from '@/api/invoicerecord.js';
	import emptyPage from '@/components/emptyPage.vue';
	import atModel from '@/components/accredit/index.vue';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		mapGetters
	} from "vuex";
	let app = getApp();
	export default {
		components: {
			emptyPage,
			atModel
		},
		data() {
			return {
        urlDomain: this.$Cache.get("imgHost"),
				invoicerecordList: [],
				loading: false,
				loadend: false,
				loadTitle: '加载更多',
				page: 1,
				limit: 20,
				theme: app.globalData.theme,
			};
		},
		computed: mapGetters(['isLogin']),
		onLoad(options) {
			if (this.isLogin) {
				this.getInvoicerecordList(true);
			} else {
				toLogin();
			}
		},
		onShow: function() {
			let that = this;
			that.getInvoicerecordList(true);
		},
		methods: {
			/**
			 * 获取开票列表
			 *
			 */
       getInvoicerecordList: function(isPage) {
				let that = this;
				if (that.loading) return;
				that.loading = true;
				getInvoicerecord().then(res => {
					that.invoicerecordList = res.data;
					that.loading = false;
				}).catch(err => {
					that.loading = false;
				});
			},
			/**
			 * 编辑地址
			 */
			editInvoice: function(id,businessAddress) {
        if(businessAddress){
          uni.navigateTo({
            url: `/pages/users/user_bind_invoice_create/index?id=${id}&businessAddress=1`
          })
        }else{
          uni.navigateTo({
            url: `/pages/users/user_bind_invoice_create/index?id=${id}`
          })
        }
			},
			/**
			 * 删除地址
			 */
			delInvoice: function(index) {
				let that = this,
					invoice = this.invoicerecordList[index];
				if (invoice == undefined) return that.$util.Tips({
					title: '您删除的开票信息不存在!'
				});
				uni.showModal({
					content: '确定删除该开票信息',
					cancelText: "取消", 
					confirmText: "确定", 
					showCancel: true,
					confirmColor: '#f55850',
					success: (res) => {
						if (res.confirm) {
							delInvoicerecord(invoice.id).then(res => {
								that.invoicerecordList.splice(index, 1);
								that.$set(that, 'invoicerecordList', that.invoicerecordList);
								that.$util.Tips({
									title: '删除成功',
									icon: 'success'
								});
							}).catch(err => {
								return that.$util.Tips({
									title: err
								});
							});
						} else {

						}
					},
				})
			},
			/**
			 * 新增地址
			 */
			addInvoice: function() {
				uni.navigateTo({
					url: '/pages/users/user_bind_invoice_create/index'
				})
			},
		},
		onReachBottom: function() {
			this.getInvoicerecordList();
		}
	}
</script>

<style lang="scss" scoped>
	.address-management {
		padding: 20rpx 30rpx;
	}

	.address-management.fff {
		background-color: #fff;
		height: 1300rpx
	}

	.bg_color {
		@include main_bg_color(theme);
	}

	.line {
		width: 100%;
		height: 3rpx;

		image {
			width: 100%;
			height: 100%;
			display: block;
		}
	}

	.address-management .item {
		background-color: #fff;
		padding: 0 20rpx;
		margin-bottom: 20rpx;
	}

	.address-management .item .address {
		padding: 35rpx 0;
		border-bottom: 1rpx solid #eee;
		font-size: 28rpx;
		color: #282828;
	}

	.address-management .item .address .consignee {
		font-size: 28rpx;
		font-weight: bold;
		margin-bottom: 8rpx;
	}

	.address-management .item .address .consignee .phone {
		margin-left: 25rpx;
	}

	.address-management .item .operation {
		height: 83rpx;
		font-size: 28rpx;
		color: #282828;
	}

	.address-management .item .operation .radio text {
		margin-left: 13rpx;
	}

	.address-management .item .operation .iconfont {
		color: #2c2c2c;
		font-size: 35rpx;
		vertical-align: -2rpx;
		margin-right: 10rpx;
	}

	.address-management .item .operation .iconfont.icon-shanchu {
		margin-left: 35rpx;
		font-size: 38rpx;
	}

	.footer {
		position: fixed;
		width: 100%;
		background-color: #fff;
		bottom: 0;
		height: 106rpx;
		padding: 0 30rpx;
		box-sizing: border-box;
	}

	.footer .addressBnt {
		width: 100%;
		height: 76rpx;
		border-radius: 50rpx;
		text-align: center;
		line-height: 76rpx;
		font-size: 30rpx;
		color: #fff;
	}

	.footer .addressBnt.on {
		width: 690rpx;
		margin: 0 auto;
	}

	.footer .addressBnt .iconfont {
		font-size: 35rpx;
		margin-right: 8rpx;
		vertical-align: -1rpx;
	}

	.footer .addressBnt.wxbnt {
		@include left_color(theme);
	}

	/deep/ radio .wx-radio-input.wx-radio-input-checked {
		@include main_bg_color(theme);
		@include coupons_border_color(theme);
	}

	/deep/ radio .uni-radio-input.uni-radio-input-checked {
		@include main_bg_color(theme);
		border: none !important;
	}
</style>
